package jsu.blogger.controller;

import jakarta.servlet.http.HttpServletRequest;
import jsu.blogger.pojo.Result;
import jsu.blogger.pojo.Tag;
import jsu.blogger.service.impl.SetUserDataServiceImpl;
import jsu.blogger.util.ReturnResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class SetUserDataController {
    @Autowired
    private SetUserDataServiceImpl setUserDataService;

    @PostMapping("/setUsername")
    public Result setUsername(HttpServletRequest request){
        Result result= ReturnResultUtil.failure("修改失败");
        Map<String,Object> map = (Map<String, Object>) request.getAttribute("requestBody");
        // 安全获取 userId
        Object userIdObj = map.get("userId");
        Long userId = null;
        userId = getaLong(userIdObj, userId);
        String newUsername= (String) map.get("newUsername");

        int i=setUserDataService.setUsernameByUserId(newUsername,userId);

        if(i==1){
            result=ReturnResultUtil.success("修改用户名成功");
        }
        return result;
    }

    @PostMapping("/setSex")
    public Result setSex(HttpServletRequest request){
        Result result=ReturnResultUtil.failure("修改失败");
        Map<String,Object> map = (Map<String, Object>) request.getAttribute("requestBody");
        // 安全获取 userId
        Object userIdObj = map.get("userId");
        Long userId = null;
        userId = getaLong(userIdObj, userId);
        int newSex= (int) map.get("sex");

        int i=setUserDataService.setSexByUserId(newSex,userId);
        if(i==1){
            result=ReturnResultUtil.success();
        }

        return result;
    }

    @PostMapping("/img/setAvatar")
    public Result setAvatar(@RequestParam("avatar") MultipartFile avatar,
                            @RequestParam("userId") Long userId,
                            @RequestParam("msgType") String msgType){
        Result result=ReturnResultUtil.failure("头像上传失败");

        if (!avatar.isEmpty()) {
            int i=setUserDataService.setAvatarByUserId(avatar,userId);
            if(i==1){
                result=ReturnResultUtil.success("上传成功");
            }
        }else {
            System.out.println("文件为空");
        }

        return result;
    }

    @PostMapping("/interestsAlter")
    public Result interestsAlter(HttpServletRequest request){
        Result result=ReturnResultUtil.failure("未知错误");
        Map<String,Object> map = (Map<String, Object>) request.getAttribute("requestBody");
        // 安全获取 userId
        Object userIdObj = map.get("userId");
        Long userId = null;
        userId = getaLong(userIdObj, userId);
        List<Map<String,Object>> objList= (List<Map<String, Object>>) map.get("newInterests");
        //System.out.println(obj);
        List<Tag> tags=new ArrayList<>();
        for (Map<String, Object> obj :
                objList) {
            String tagName = (String) obj.get("tagName");
            Long tagId=null;
            tagId= getaLong(obj.get("tagId"),tagId) ;
            tags.add(new Tag(tagId,tagName));
        }
        //System.out.println(tags);
        int re=setUserDataService.setUserTags(tags,userId);
        if(re>0){
            result=ReturnResultUtil.success("修改成功");
        }
        return result;
    }

    @PostMapping("/creatFolder")
    public Result creatFolder(HttpServletRequest request){
        Result result=ReturnResultUtil.failure("未知错误");
        Map<String,Object> map = (Map<String, Object>) request.getAttribute("requestBody");
        // 安全获取 userId
        Object userIdObj = map.get("userId");
        Long userId = null;
        userId = getaLong(userIdObj, userId);

        String newFolderName= (String) map.get("folderName");
        if(setUserDataService.creatFolder(userId,newFolderName)==1){
            result=ReturnResultUtil.success("创建成功");
        }
        return result;
    }


    private Long getaLong(Object userIdObj, Long userId) {
        if (userIdObj instanceof Integer) {
            // 如果 userId 是 Integer 类型
            userId = ((Integer) userIdObj).longValue();
        } else if (userIdObj instanceof Long) {
            // 如果 userId 是 Long 类型
            userId = (Long) userIdObj;
        } else {
            System.out.println("userId 类型不匹配: " + userIdObj.getClass().getName());
        }
        return userId;
    }
}
